home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 October: Technology Seed / ADC Seed CD - October 1999.toast / FireWire / FireWire_2.1_SDK_DR3 / Source / SBP2 / SBP2DemoApp / SBP2DemoApp.c next >
Encoding:
C/C++ Source or Header  |  1999-05-17  |  51.7 KB  |  2,254 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        SBP2DemoApp.c
  3.  
  4.     Contains:    A simple application to demonstrate using the SBP-2 sample driver.
  5.  
  6.     Version:    1.0
  7.  
  8.     Copyright:    © 1998-1999 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                Eric Anderson
  13.  
  14.         Other Contact:        
  15.  
  16.         Technology:            FireWire
  17.  
  18.     Writers:
  19.  
  20.         (EA)    Eric Anderson (ewa)
  21.  
  22.     Change History (most recent first):
  23.  
  24.        <FW7>     1/10/99    EA        Added support for testing cable power management API.
  25.        <FW6>      1/1/99    EA        Added test of FWGetFWReferenceIDFromUniqueID when we query
  26.                                     logins.
  27.        <FW5>    12/31/98    EA        Added support for Query Logins.
  28.        <FW4>    12/31/98    EA        Added support for sending a TARGET RESET management ORB.
  29.        <FW3>    11/18/98    EA        Changed to show our LUN value.
  30.        <FW2>     9/20/98    EA        Filled in header comments.
  31.        <FW1>     9/20/98    EA        first checked in
  32. */
  33.  
  34.  
  35. #include <Types.h>
  36. #include <Memory.h>
  37. #include <Devices.h>
  38. #include <Resources.h>
  39. #include <QuickDraw.h>
  40. #include <Fonts.h>
  41. #include <Events.h>
  42. #include <AppleEvents.h>
  43. #include <EPPC.h>
  44. #include <Windows.h>
  45. #include <Menus.h>
  46. #include <Dialogs.h>
  47. #include <ToolUtils.h>
  48. #include <DiskInit.h>
  49. #include <FireWireSBP2.h>
  50. #include <SampleSBP2.h>
  51. #include <SBP2DemoApp.h>
  52.  
  53. #include <TextUtils.h>
  54. #include <stdio.h>
  55. char    debugStr[256];
  56.  
  57. ////////////////////////////////////////////////////////////////////////////////
  58. //
  59. // Define global structure to hold all of the SBP2Demo data.
  60. //
  61.  
  62. SBP2DemoAppDataPtr                gpSBP2DemoAppData = nil;
  63. Boolean                            quitSBP2DemoApp = false;
  64.  
  65.  
  66. ////////////////////////////////////////////////////////////////////////////////
  67. //
  68. // Internal procedure prototypes.
  69. //
  70.  
  71. static void    SBP2DemoEventLoop (void);
  72.  
  73. static OSErr    HandleEvent (
  74.     EventRecord                    *pEventRecord);
  75.  
  76. static OSErr    HandleMouseDownEvent (
  77.     EventRecord                    *pEventRecord);
  78.  
  79. static OSErr    HandleKeyEvent (
  80.     EventRecord                    *pEventRecord);
  81.  
  82. static OSErr    HandleOSEvent (
  83.     EventRecord                    *pEventRecord);
  84.  
  85. static OSErr    HandleMenuCommand (
  86.     long                        menuCommand);
  87.  
  88. static OSErr    HandleAppleMenuCommand (
  89.     short                        menuItem);
  90.  
  91. static OSErr    HandleFileMenuCommand (
  92.     short                        menuItem);
  93.  
  94. static OSErr    HandleCommandMenuCommand (
  95.     short                        menuItem);
  96.  
  97. static OSErr    HandleCloseFileMenuCommand (void);
  98.  
  99. static OSErr    HandleLoginMenuCommand (void);
  100.  
  101. static OSErr    HandleLogoutMenuCommand (void);
  102.  
  103. static OSErr    HandleQueryLoginsMenuCommand (void);
  104.  
  105. static OSErr    HandleStatusInquiryMenuCommand (void);
  106.  
  107. static OSErr    HandleModeSenseMenuCommand (void);
  108.  
  109. static OSErr    HandleReadBlockMenuCommand (void);
  110.  
  111. static OSErr    HandleTargetResetMenuCommand (void);
  112.  
  113. static OSErr    HandlePowerTestMenuCommand (void);
  114.  
  115. static OSErr    HandleWindowCommand (
  116.     WindowRef                    windowRef);
  117.  
  118. static OSErr    SBP2DemoInitialize (void);
  119.  
  120. static OSErr    QuitApplication (void);
  121.  
  122. static OSErr    CreateSBP2Demo (
  123.     SBPDriverID                    sbpDriverID);
  124.  
  125. static OSErr    RemoveSBP2Demo (
  126.     SBPDriverID                    sbpDriverID);
  127.  
  128. static OSErr    CloseSBP2DemoWindow (
  129.     WindowRef                    windowRef);
  130.  
  131. static OSErr    DrawSBP2Demo (
  132.     WindowRef                    windowRef);
  133.  
  134. static OSErr    UpdateWindow (
  135.     WindowRef                    windowRef);
  136.  
  137. static OSErr    UpdateMenus (void);
  138.  
  139. static OSErr    UpdateCursor (
  140.     Point                        mousePosition,
  141.     RgnHandle                    hCursorRgn);
  142.  
  143. static Boolean    IsSBP2DemoWindow (
  144.     WindowRef                    windowRef);
  145.  
  146. static void    GetMousePosition (
  147.     Point                        *pMousePosition);
  148.  
  149. static pascal OSErr    HandleOpenApplicationEvent (
  150.     AppleEvent                    *theAppleEvent,
  151.     AppleEvent                    *reply,
  152.     long                        handlerRefcon);
  153.  
  154. static pascal OSErr    HandleOpenDocumentsEvent (
  155.     AppleEvent                    *theAppleEvent,
  156.     AppleEvent                    *reply,
  157.     long                        handlerRefcon);
  158.  
  159. static pascal OSErr    HandlePrintDocumentsEvent (
  160.     AppleEvent                    *theAppleEvent,
  161.     AppleEvent                    *reply,
  162.     long                        handlerRefcon);
  163.  
  164. static pascal OSErr    HandleQuitApplicationEvent (
  165.     AppleEvent                    *theAppleEvent,
  166.     AppleEvent                    *reply,
  167.     long                        handlerRefcon);
  168.  
  169. static pascal OSErr    HandleDeviceAddedEvent (
  170.     AppleEvent                    *theAppleEvent,
  171.     AppleEvent                    *reply,
  172.     long                        handlerRefcon);
  173.  
  174. static pascal OSErr    HandleDeviceRemovedEvent (
  175.     AppleEvent                    *theAppleEvent,
  176.     AppleEvent                    *reply,
  177.     long                        handlerRefcon);
  178.  
  179. static SBP2DemoDataPtr    FindSBP2DemoFromSBPDriver (
  180.     SBPDriverID                    sbpDriverID);
  181.  
  182. static OSErr    RemoveSBP2DemoFromList (
  183.     SBP2DemoDataPtr            pSBP2DemoData);
  184.  
  185. static OSErr    PrepareWindowForUpdating (
  186.     WindowRef                    windowRef,
  187.     Boolean                        *pUpdateNeeded);
  188.  
  189. static OSErr    ReleaseWindowFromUpdating (
  190.     WindowRef                    windowRef);
  191.  
  192. static void    CheckAllDevices (void);
  193.  
  194.  
  195. ////////////////////////////////////////////////////////////////////////////////
  196. ////////////////////////////////////////////////////////////////////////////////
  197. //
  198. // Exported routines.
  199. //
  200. ////////////////////////////////////////////////////////////////////////////////
  201. ////////////////////////////////////////////////////////////////////////////////
  202.  
  203. ////////////////////////////////////////////////////////////////////////////////
  204. //
  205. // SBP2DemoMain
  206. //
  207. //   This routine is the main entry point for the SBPSBP2Demo
  208. // application.
  209. //
  210.  
  211. SBP2DemoMain()
  212. {
  213.     OSErr                        err = noErr;
  214.  
  215.     // Initialize the application.
  216.     err = SBP2DemoInitialize ();
  217.  
  218.     // Main application event loop.
  219.     if (err == noErr)
  220.         SBP2DemoEventLoop ();
  221.  
  222.     // Dispose of app data.
  223.     if (gpSBP2DemoAppData != nil)
  224.         QuitApplication ();
  225.  
  226.     return (err);
  227. }
  228.  
  229.  
  230. ////////////////////////////////////////////////////////////////////////////////
  231. ////////////////////////////////////////////////////////////////////////////////
  232. //
  233. // Internal routines.
  234. //
  235. ////////////////////////////////////////////////////////////////////////////////
  236. ////////////////////////////////////////////////////////////////////////////////
  237.  
  238. ////////////////////////////////////////////////////////////////////////////////
  239. //
  240. // SBP2DemoEventLoop
  241. //
  242. //   This routine is the main event loop for the SBPSBP2Demo
  243. // application.
  244. //
  245.  
  246. static void    SBP2DemoEventLoop(void)
  247. {
  248.     RgnHandle                    hCursorRgn;
  249.     Point                        mousePosition;
  250.     EventRecord                    eventRecord;
  251.     Boolean                        gotEvent;
  252.     OSErr                        err = noErr;
  253.  
  254.     // Initialize cursor region.
  255.     hCursorRgn = NewRgn ();
  256.     if (hCursorRgn == nil)
  257.         err = memFullErr;//zzz is this right?
  258.  
  259.     // Main event loop.
  260.     while ((!quitSBP2DemoApp) && (err == noErr))
  261.     {
  262.         // Update cursor to correct image.
  263.         GetMousePosition (&mousePosition);
  264.         err = UpdateCursor (mousePosition, hCursorRgn);
  265.  
  266.         // Get any SBP events.
  267.         if (err == noErr)
  268.             err = GetNextSBPClientEvent (gpSBP2DemoAppData->sbpClientID);
  269.  
  270.         // Wait for next event.
  271.         if (err == noErr)
  272.             gotEvent = WaitNextEvent (everyEvent, &eventRecord, 1, hCursorRgn);
  273.  
  274.         // Handle event if we got one.
  275.         if (err == noErr)
  276.         {
  277.             if (gotEvent)
  278.             {
  279.                 // Update cursor to correct image.
  280.                 err = UpdateCursor (eventRecord.where, hCursorRgn);
  281.     
  282.                 // Handle the event.
  283.                 if (err == noErr)
  284.                     err = HandleEvent (&eventRecord);
  285.             }
  286.         }
  287.         
  288.         // Check status of all SBP devices.
  289.         // This is a bad design - we are polling every time, even if nothing changes.
  290.         if (!quitSBP2DemoApp) CheckAllDevices ();
  291.     }
  292. }
  293.  
  294.  
  295. ////////////////////////////////////////////////////////////////////////////////
  296. //
  297. // HandleEvent
  298. //
  299. //   This routine handles the given event.
  300. //
  301.  
  302. static OSErr    HandleEvent(
  303.     EventRecord                    *pEventRecord)
  304. {
  305.     Point                        point;
  306.     OSErr                        err = noErr;
  307.  
  308.     // Main event dispatcher.
  309.     switch (pEventRecord->what)
  310.     {
  311.         case mouseDown :
  312.             err = HandleMouseDownEvent (pEventRecord);
  313.             break;
  314.  
  315.         case keyDown :
  316.         case autoKey :
  317.             err = HandleKeyEvent (pEventRecord);
  318.             break;
  319.  
  320.         case updateEvt :
  321.             err = UpdateWindow ((WindowRef) (pEventRecord->message));
  322.             break;
  323.  
  324.         case diskEvt :
  325.             if ((pEventRecord->message >> 16) != noErr)
  326.             {
  327.                 SetPt (&point, kDILeft, kDITop);
  328.                 err = DIBadMount (point, pEventRecord->message);
  329.             }
  330.             break;
  331.  
  332.         case osEvt :
  333.             err = HandleOSEvent (pEventRecord);
  334.             break;
  335.  
  336.         case kHighLevelEvent :
  337.             AEProcessAppleEvent (pEventRecord);
  338.             break;
  339.  
  340.         default :
  341.             break;
  342.     }
  343.  
  344.     return (err);
  345. }
  346.  
  347.  
  348. ////////////////////////////////////////////////////////////////////////////////
  349. //
  350. // HandleMouseDownEvent
  351. //
  352. //   This routine handles mouse down events.
  353. //
  354.  
  355. static OSErr    HandleMouseDownEvent(
  356.     EventRecord                    *pEventRecord)
  357. {
  358.     WindowRef                    windowRef;
  359.     short                        inPart;
  360.     OSErr                        err = noErr;
  361.  
  362.     // Find out what part of application mouse is in.
  363.     inPart = FindWindow (pEventRecord->where, &windowRef);
  364.  
  365.     // Dispatch off of inPart.
  366.     switch (inPart)
  367.     {
  368.         case inMenuBar :
  369.             UpdateMenus ();
  370.             err = HandleMenuCommand (MenuSelect (pEventRecord->where));
  371.             break;
  372.  
  373.         case inSysWindow :
  374.             SystemClick (pEventRecord, windowRef);
  375.             break;
  376.  
  377.         case inContent :
  378.             if (windowRef == FrontWindow ())
  379.                 err = HandleWindowCommand (windowRef);
  380.             else
  381.                 SelectWindow (windowRef);
  382.             break;
  383.  
  384.         case inDrag :
  385.             DragWindow
  386.                 (windowRef,
  387.                  pEventRecord->where,
  388.                  &(gpSBP2DemoAppData->qdGlobals.screenBits.bounds));
  389.             break;
  390.  
  391.         case inGoAway :
  392.             if (TrackGoAway (windowRef, pEventRecord->where))
  393.                 CloseSBP2DemoWindow (windowRef);
  394.             break;
  395.  
  396.         default :
  397.             break;
  398.     }
  399.  
  400.     return (err);
  401. }
  402.  
  403.  
  404. ////////////////////////////////////////////////////////////////////////////////
  405. //
  406. // HandleKeyEvent
  407. //
  408. //   This routine handles key events.
  409. //
  410.  
  411. static OSErr    HandleKeyEvent(
  412.     EventRecord                    *pEventRecord)
  413. {
  414.     char                        key;
  415.     OSErr                        err = noErr;
  416.  
  417.     // Read the key from the event record.
  418.     key = pEventRecord->message & charCodeMask;
  419.  
  420.     // We're only interested in command key down events.
  421.     if ((pEventRecord->modifiers & cmdKey) &&
  422.         (pEventRecord->what == keyDown))
  423.     {
  424.         UpdateMenus ();
  425.         err = HandleMenuCommand (MenuKey (key));
  426.     }
  427.  
  428.     return (err);
  429. }
  430.  
  431.  
  432. ////////////////////////////////////////////////////////////////////////////////
  433. //
  434. // HandleOSEvent
  435. //
  436. //   This routine handles OS events.
  437. //
  438.  
  439. static OSErr    HandleOSEvent(
  440.     EventRecord                    *pEventRecord)
  441. {
  442.     UInt32                        osEventType;
  443.     OSErr                        err = noErr;
  444.  
  445.     // Get the OS event type.
  446.     osEventType = pEventRecord->message >> 24;
  447.  
  448.     // Dispatch.
  449.     switch (osEventType)
  450.     {
  451.         case suspendResumeMessage :
  452.             if (pEventRecord->message & resumeFlag)
  453.                 gpSBP2DemoAppData->inForeground = true;
  454.             else
  455.                 gpSBP2DemoAppData->inForeground = false;
  456.             break;
  457.  
  458.         default :
  459.             break;
  460.     }
  461.  
  462.     return (err);
  463. }
  464.  
  465.  
  466. ////////////////////////////////////////////////////////////////////////////////
  467. //
  468. // HandleMenuCommand
  469. //
  470. //   This routine handles menu commands.
  471. //
  472.  
  473. static OSErr    HandleMenuCommand(
  474.     long                        menuCommand)
  475. {
  476.     short                        menuID,
  477.                                 menuItem;
  478.     OSErr                        err = noErr;
  479.  
  480.     // Get menu ID and item number from the menu command.
  481.     menuID = menuCommand >> 16;
  482.     menuItem = menuCommand & 0xFFFF;
  483.  
  484.     // Dispatch off of menu ID.
  485.     switch (menuID)
  486.     {
  487.         case kAppleMenuID :
  488.             err = HandleAppleMenuCommand (menuItem);
  489.             break;
  490.  
  491.         case kFileMenuID :
  492.             err = HandleFileMenuCommand (menuItem);
  493.             break;
  494.  
  495.         case kEditMenuID :
  496.             // Don't know if we'll ever get here.
  497.             SystemEdit (menuItem - 1);
  498.             break;
  499.  
  500.         case kCommandMenuID :
  501.             err = HandleCommandMenuCommand (menuItem);
  502.             break;
  503.  
  504.         default :
  505.             break;
  506.     }
  507.  
  508.     // Unhilite menu.
  509.     HiliteMenu (0);
  510.  
  511.     return (err);
  512. }
  513.  
  514.  
  515. ////////////////////////////////////////////////////////////////////////////////
  516. //
  517. // HandleAppleMenuCommand
  518. //
  519. //   This routine handles apple menu commands.
  520. //
  521.  
  522. static OSErr    HandleAppleMenuCommand(
  523.     short                        menuItem)
  524. {
  525.     Str255                        daName;
  526.     OSErr                        err = noErr;
  527.  
  528.     // Apple menu dispatch.
  529.     switch (menuItem)
  530.     {
  531.         case kAboutAppleMenuItem :
  532.             Alert (kAboutAlertResourceID, nil);
  533.             break;
  534.  
  535.         default :
  536.             // All non-about items are DAs.
  537.             GetMenuItemText (GetMenuHandle (kAppleMenuID), menuItem, daName);
  538.             OpenDeskAcc (daName);
  539.             break;
  540.     }
  541.  
  542.     return (err);
  543. }
  544.  
  545.  
  546. ////////////////////////////////////////////////////////////////////////////////
  547. //
  548. // HandleFileMenuCommand
  549. //
  550. //   This routine handles file menu commands.
  551. //
  552.  
  553. static OSErr    HandleFileMenuCommand(
  554.     short                        menuItem)
  555. {
  556.     OSErr                        err = noErr;
  557.  
  558.     // File menu dispatch.
  559.     switch (menuItem)
  560.     {
  561.         case kCloseFileMenuItem :
  562.             HandleCloseFileMenuCommand ();
  563.             break;
  564.  
  565.         case kQuitFileMenuItem :
  566.             QuitApplication ();
  567.             break;
  568.  
  569.         default :
  570.             break;
  571.     }
  572.  
  573.     return (err);
  574. }
  575.  
  576.  
  577. ////////////////////////////////////////////////////////////////////////////////
  578. //
  579. // HandleCommandMenuCommand
  580. //
  581. //   This routine handles extra menu commands.
  582. //
  583.  
  584. static OSErr    HandleCommandMenuCommand(
  585.     short                        menuItem)
  586. {
  587.     OSErr                        err = noErr;
  588.  
  589.     // File menu dispatch.
  590.     switch (menuItem)
  591.     {
  592.         case kLoginMenuItem:
  593.             HandleLoginMenuCommand ();
  594.             break;
  595.             
  596.         case kLogoutMenuItem:
  597.             HandleLogoutMenuCommand ();
  598.             break;
  599.         
  600.         case kQueryLoginsMenuItem:
  601.             HandleQueryLoginsMenuCommand ();
  602.             break;
  603.         
  604.         case kStatusInquiryMenuItem:
  605.             HandleStatusInquiryMenuCommand ();
  606.             break;
  607.             
  608.         case kModeSenseMenuItem:
  609.             HandleModeSenseMenuCommand ();
  610.             break;
  611.             
  612.         case kReadBlockMenuItem:
  613.             HandleReadBlockMenuCommand ();
  614.             break;
  615.             
  616.         case kTargetResetMenuItem:
  617.             HandleTargetResetMenuCommand ();
  618.             break;
  619.             
  620.         case kPowerTestMenuItem:
  621.             HandlePowerTestMenuCommand ();
  622.             break;
  623.             
  624.         default :
  625.             break;
  626.     }
  627.  
  628.     return (err);
  629. }
  630.  
  631.  
  632. ////////////////////////////////////////////////////////////////////////////////
  633. //
  634. // HandleCloseFileMenuCommand
  635. //
  636. //   This routine handles the close command.
  637. //
  638.  
  639. static OSErr    HandleCloseFileMenuCommand(void)
  640. {
  641.     WindowRef                    windowRef;
  642.     OSErr                        err = noErr;
  643.  
  644.     // Get reference to front most window and close.
  645.     windowRef = FrontWindow ();
  646.     if (windowRef != kInvalidWindowRef)
  647.         err = CloseSBP2DemoWindow (windowRef);
  648.     
  649.     return (err);
  650. }
  651.  
  652.  
  653. ////////////////////////////////////////////////////////////////////////////////
  654. //
  655. // HandleLoginMenuCommand
  656. //
  657. //   This routine handles the login command.
  658. //
  659.  
  660. static OSErr    HandleLoginMenuCommand (void)
  661. {
  662.     WindowRef                    windowRef;
  663.     SBP2DemoDataPtr                pSBP2DemoData;
  664.     SBPLoginParams                sbpLoginParams;
  665.     GrafPtr                        pWindowPort;
  666.     OSErr                        err = noErr;
  667.  
  668.     // Get reference to front most window.
  669.     windowRef = FrontWindow ();
  670.  
  671.     if (windowRef != kInvalidWindowRef)
  672.     {
  673.         // Get device data.
  674.         pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  675.         pSBP2DemoData->lastCommand = kLoginMenuItem;
  676.         
  677.         sbpLoginParams.sbpInterfaceParams.interfaceSelector = kSampleSBPLogin;
  678.         sbpLoginParams.responseBufferPtr = 0;
  679.         sbpLoginParams.responseBufferSize = 0;
  680.  
  681.         err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
  682.                              (SBPInterfaceParamsPtr) &sbpLoginParams);
  683.  
  684.         pSBP2DemoData->lastError = sbpLoginParams.status;
  685.         
  686.         if (sbpLoginParams.status == noErr)
  687.             pSBP2DemoData->login = true;
  688.         
  689.         // Force window update to show command status.
  690.         pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  691.         SetPortWindowPort (windowRef);
  692.         InvalRect (&(pWindowPort->portRect));
  693.     }
  694.     
  695.     return err;
  696. }
  697.  
  698.  
  699. ////////////////////////////////////////////////////////////////////////////////
  700. //
  701. // HandleLogoutMenuCommand
  702. //
  703. //   This routine handles the logout command.
  704. //
  705.  
  706. static OSErr    HandleLogoutMenuCommand (void)
  707. {
  708.     WindowRef                    windowRef;
  709.     SBP2DemoDataPtr                pSBP2DemoData;
  710.     SBPLogoutParams                sbpLogoutParams;
  711.     GrafPtr                        pWindowPort;
  712.     OSErr                        err = noErr;
  713.  
  714.     // Get reference to front most window.
  715.     windowRef = FrontWindow ();
  716.  
  717.     if (windowRef != kInvalidWindowRef)
  718.     {
  719.         // Get device data.
  720.         pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  721.         pSBP2DemoData->lastCommand = kLogoutMenuItem;
  722.         
  723.         sbpLogoutParams.sbpInterfaceParams.interfaceSelector = kSampleSBPLogout;
  724.  
  725.         err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
  726.                              (SBPInterfaceParamsPtr) &sbpLogoutParams);
  727.  
  728.         pSBP2DemoData->lastError = sbpLogoutParams.status;
  729.         
  730.         if (sbpLogoutParams.status == noErr)
  731.             pSBP2DemoData->login = false;
  732.         
  733.         // Force window update to show command status.
  734.         pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  735.         SetPortWindowPort (windowRef);
  736.         InvalRect (&(pWindowPort->portRect));
  737.     }
  738.     
  739.     return err;
  740. }
  741.  
  742.  
  743. ////////////////////////////////////////////////////////////////////////////////
  744. //
  745. // HandleQueryLoginsMenuCommand
  746. //
  747. //   This routine handles the query logins command.
  748. //
  749.  
  750. static OSErr    HandleQueryLoginsMenuCommand (void)
  751. {
  752.     WindowRef                    windowRef;
  753.     SBP2DemoDataPtr                pSBP2DemoData;
  754.     SBPQueryLoginsParamsPtr        pSBPQueryLoginsParams;
  755.     GrafPtr                        pWindowPort;
  756.     OSErr                        err = noErr;
  757.  
  758.     // Get reference to front most window.
  759.     windowRef = FrontWindow ();
  760.  
  761.     if (windowRef != kInvalidWindowRef)
  762.     {
  763.         // Get device data.
  764.         pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  765.         pSBP2DemoData->lastCommand = kQueryLoginsMenuItem;
  766.         pSBPQueryLoginsParams = &(pSBP2DemoData->sbpQueryLoginsParams);
  767.         
  768.         pSBPQueryLoginsParams->sbpInterfaceParams.interfaceSelector = kSampleSBPQueryLogins;
  769.  
  770.         err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
  771.                              (SBPInterfaceParamsPtr) pSBPQueryLoginsParams);
  772.  
  773.         pSBP2DemoData->lastError = pSBPQueryLoginsParams->status;
  774.                 
  775.         // Force window update to show command status.
  776.         pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  777.         SetPortWindowPort (windowRef);
  778.         InvalRect (&(pWindowPort->portRect));
  779.     }
  780.     
  781.     return err;
  782. }
  783.  
  784.  
  785. ////////////////////////////////////////////////////////////////////////////////
  786. //
  787. // HandleStatusInquiryMenuCommand
  788. //
  789. //   This routine handles the status inquiry command.
  790. //
  791.  
  792. static OSErr    HandleStatusInquiryMenuCommand (void)
  793. {
  794.     WindowRef                    windowRef;
  795.     SBP2DemoDataPtr                pSBP2DemoData;
  796.     SBPStatusInquiryParamsPtr    pSBPStatusInquiryParams;
  797.     GrafPtr                        pWindowPort;
  798.     OSErr                        err = noErr;
  799.  
  800.     // Get reference to front most window.
  801.     windowRef = FrontWindow ();
  802.  
  803.     if (windowRef != kInvalidWindowRef)
  804.     {
  805.         // Get device data.
  806.         pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  807.         pSBP2DemoData->lastCommand = kStatusInquiryMenuItem;
  808.         pSBPStatusInquiryParams = &(pSBP2DemoData->sbpStatusInquiryParams);
  809.         
  810.         pSBPStatusInquiryParams->sbpInterfaceParams.interfaceSelector = kSampleSBPStatusInquiry;
  811.  
  812.         err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
  813.                              (SBPInterfaceParamsPtr) pSBPStatusInquiryParams);
  814.  
  815.         pSBP2DemoData->lastError = pSBPStatusInquiryParams->status;
  816.                 
  817.         // Force window update to show command status.
  818.         pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  819.         SetPortWindowPort (windowRef);
  820.         InvalRect (&(pWindowPort->portRect));
  821.     }
  822.     
  823.     return err;
  824. }
  825.  
  826.  
  827. ////////////////////////////////////////////////////////////////////////////////
  828. //
  829. // HandleModeSenseMenuCommand
  830. //
  831. //   This routine handles the mode sense command.
  832. //
  833.  
  834. static OSErr    HandleModeSenseMenuCommand (void)
  835. {
  836.     WindowRef                    windowRef;
  837.     SBP2DemoDataPtr                pSBP2DemoData;
  838.     SBPModeSenseParamsPtr        pSBPModeSenseParams;
  839.     GrafPtr                        pWindowPort;
  840.     OSErr                        err = noErr;
  841.  
  842.     // Get reference to front most window.
  843.     windowRef = FrontWindow ();
  844.  
  845.     if (windowRef != kInvalidWindowRef)
  846.     {
  847.         // Get device data.
  848.         pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  849.         pSBP2DemoData->lastCommand = kModeSenseMenuItem;
  850.         pSBPModeSenseParams = &(pSBP2DemoData->sbpModeSenseParams);
  851.         
  852.         pSBPModeSenseParams->sbpInterfaceParams.interfaceSelector = kSampleSBPModeSense;
  853.  
  854.         err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
  855.                              (SBPInterfaceParamsPtr) pSBPModeSenseParams);
  856.  
  857.         pSBP2DemoData->lastError = pSBPModeSenseParams->status;
  858.                 
  859.         // Force window update to show command status.
  860.         pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  861.         SetPortWindowPort (windowRef);
  862.         InvalRect (&(pWindowPort->portRect));
  863.     }
  864.     
  865.     return err;
  866. }
  867.  
  868.  
  869. ////////////////////////////////////////////////////////////////////////////////
  870. //
  871. // HandleReadBlockMenuCommand
  872. //
  873. //   This routine handles the read block command.
  874. //
  875.  
  876. static OSErr    HandleReadBlockMenuCommand (void)
  877. {
  878.     WindowRef                    windowRef;
  879.     SBP2DemoDataPtr                pSBP2DemoData;
  880.     SBPReadBlockParamsPtr        pSBPReadBlockParams;
  881.     GrafPtr                        pWindowPort;
  882.     OSErr                        err = noErr;
  883.  
  884.     // Get reference to front most window.
  885.     windowRef = FrontWindow ();
  886.  
  887.     if (windowRef != kInvalidWindowRef)
  888.     {
  889.         // Get device data.
  890.         pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  891.         pSBP2DemoData->lastCommand = kReadBlockMenuItem;
  892.         pSBPReadBlockParams = &(pSBP2DemoData->sbpReadBlockParams);
  893.         
  894.         pSBPReadBlockParams->sbpInterfaceParams.interfaceSelector = kSampleSBPReadBlock;
  895.  
  896.         err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
  897.                              (SBPInterfaceParamsPtr) pSBPReadBlockParams);
  898.  
  899.         pSBP2DemoData->lastError = pSBPReadBlockParams->status;
  900.                 
  901.         // Force window update to show command status.
  902.         pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  903.         SetPortWindowPort (windowRef);
  904.         InvalRect (&(pWindowPort->portRect));
  905.     }
  906.     
  907.     return err;
  908. }
  909.  
  910.  
  911. ////////////////////////////////////////////////////////////////////////////////
  912. //
  913. // HandleTargetResetMenuCommand
  914. //
  915. //   This routine handles the target reset command.
  916. //
  917.  
  918. static OSErr    HandleTargetResetMenuCommand (void)
  919. {
  920.     WindowRef                    windowRef;
  921.     SBP2DemoDataPtr                pSBP2DemoData;
  922.     SBPTargetResetParamsPtr        pSBPTargetResetParams;
  923.     GrafPtr                        pWindowPort;
  924.     OSErr                        err = noErr;
  925.  
  926.     // Get reference to front most window.
  927.     windowRef = FrontWindow ();
  928.  
  929.     if (windowRef != kInvalidWindowRef)
  930.     {
  931.         // Get device data.
  932.         pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  933.         pSBP2DemoData->lastCommand = kTargetResetMenuItem;
  934.         pSBPTargetResetParams = &(pSBP2DemoData->sbpTargetResetParams);
  935.         
  936.         pSBPTargetResetParams->sbpInterfaceParams.interfaceSelector = kSampleSBPTargetReset;
  937.  
  938.         err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
  939.                              (SBPInterfaceParamsPtr) pSBPTargetResetParams);
  940.  
  941.         pSBP2DemoData->lastError = pSBPTargetResetParams->status;
  942.                 
  943.         // Force window update to show command status.
  944.         pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  945.         SetPortWindowPort (windowRef);
  946.         InvalRect (&(pWindowPort->portRect));
  947.     }
  948.     
  949.     return err;
  950. }
  951.  
  952.  
  953. ////////////////////////////////////////////////////////////////////////////////
  954. //
  955. // HandlePowerTestMenuCommand
  956. //
  957. //   This routine handles the power test command.
  958. //
  959.  
  960. static OSErr    HandlePowerTestMenuCommand (void)
  961. {
  962.     WindowRef                    windowRef;
  963.     SBP2DemoDataPtr                pSBP2DemoData;
  964.     SBPPowerTestParamsPtr        pSBPPowerTestParams;
  965.     GrafPtr                        pWindowPort;
  966.     OSErr                        err = noErr;
  967.  
  968.     // Get reference to front most window.
  969.     windowRef = FrontWindow ();
  970.  
  971.     if (windowRef != kInvalidWindowRef)
  972.     {
  973.         // Get device data.
  974.         pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  975.         pSBP2DemoData->lastCommand = kPowerTestMenuItem;
  976.         pSBPPowerTestParams = &(pSBP2DemoData->sbpPowerTestParams);
  977.         
  978.         pSBPPowerTestParams->sbpInterfaceParams.interfaceSelector = kSampleSBPPowerTest;
  979.  
  980.         err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
  981.                              (SBPInterfaceParamsPtr) pSBPPowerTestParams);
  982.  
  983.         pSBP2DemoData->lastError = pSBPPowerTestParams->status;
  984.                 
  985.         // Force window update to show command status.
  986.         pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  987.         SetPortWindowPort (windowRef);
  988.         InvalRect (&(pWindowPort->portRect));
  989.     }
  990.     
  991.     return err;
  992. }
  993.  
  994.  
  995. ////////////////////////////////////////////////////////////////////////////////
  996. //
  997. // HandleWindowCommand
  998. //
  999. //   This routine handles clicks in the contents of the window.
  1000. //
  1001.  
  1002. static OSErr    HandleWindowCommand(
  1003.     WindowRef                    windowRef)
  1004. {
  1005.     OSErr                        err = noErr;
  1006.  
  1007.     return (err);
  1008. }
  1009.  
  1010.  
  1011. ////////////////////////////////////////////////////////////////////////////////
  1012. //
  1013. // SBP2DemoInitialize
  1014. //
  1015. //   This routine initializes the SBPSBP2Demo application.
  1016. //
  1017.  
  1018. static OSErr    SBP2DemoInitialize(void)
  1019. {
  1020.     Handle                        hMenuBarResource;
  1021.     SBPDriverID                    *sbpDriverIDList = nil;
  1022.     UInt32                        numSBPDrivers,
  1023.                                 driverNum;
  1024.     OSErr                        err = noErr;
  1025.  
  1026.     // Create our global data record.
  1027.     gpSBP2DemoAppData = (SBP2DemoAppDataPtr)
  1028.         NewPtrClear (sizeof (SBP2DemoAppData));
  1029.     if (gpSBP2DemoAppData != nil)
  1030.     {
  1031.         gpSBP2DemoAppData->sbpClientID = kInvalidSBPClientID;
  1032.         gpSBP2DemoAppData->inForeground = true;
  1033.     }
  1034.     else
  1035.     {
  1036.         err = memFullErr;
  1037.     }
  1038.  
  1039.     // Initialize a bunch of managers.
  1040.     if (err == noErr)
  1041.     {
  1042.         InitGraf ((Ptr) &(gpSBP2DemoAppData->qdGlobals.thePort));
  1043.         InitFonts ();
  1044.         InitWindows ();
  1045.         InitMenus ();
  1046.         InitDialogs (nil);
  1047.         InitCursor ();
  1048.     }
  1049.  
  1050.     // Create our menu bar using the defs in our resources.
  1051.     if (err == noErr)
  1052.     {
  1053.         hMenuBarResource = GetNewMBar (kMenuBarResourceID);
  1054.         if (hMenuBarResource != nil)
  1055.         {
  1056.             SetMenuBar (hMenuBarResource);
  1057.             DisposeHandle (hMenuBarResource);
  1058.             AppendResMenu (GetMenuHandle (kAppleMenuID), 'DRVR');
  1059.             DrawMenuBar ();
  1060.         }
  1061.         else
  1062.         {
  1063.             err = memFullErr;//zzz what should it really be?
  1064.         }
  1065.     }
  1066.  
  1067.     // Create open application event handler.
  1068.     if (err == noErr)
  1069.     {
  1070.         gpSBP2DemoAppData->openApplicationEventHandler =
  1071.             NewAEEventHandlerProc (HandleOpenApplicationEvent);
  1072.         if (gpSBP2DemoAppData->openApplicationEventHandler == nil)
  1073.             err = memFullErr;
  1074.     }
  1075.  
  1076.     // Install handler.
  1077.     if (err == noErr)
  1078.     {
  1079.         err = AEInstallEventHandler
  1080.                 (kCoreEventClass,
  1081.                  kAEOpenApplication,
  1082.                  gpSBP2DemoAppData->openApplicationEventHandler,
  1083.                  (long) gpSBP2DemoAppData,
  1084.                  false);
  1085.         if (err == noErr)
  1086.         {
  1087.             gpSBP2DemoAppData->openApplicationEventHandlerInstalled =
  1088.                 true;
  1089.         }
  1090.     }
  1091.  
  1092.     // Create open documents event handler.
  1093.     if (err == noErr)
  1094.     {
  1095.         gpSBP2DemoAppData->openDocumentsEventHandler =
  1096.             NewAEEventHandlerProc (HandleOpenDocumentsEvent);
  1097.         if (gpSBP2DemoAppData->openDocumentsEventHandler == nil)
  1098.             err = memFullErr;
  1099.     }
  1100.  
  1101.     // Install handler.
  1102.     if (err == noErr)
  1103.     {
  1104.         err = AEInstallEventHandler
  1105.                 (kCoreEventClass,
  1106.                  kAEOpenDocuments,
  1107.                  gpSBP2DemoAppData->openDocumentsEventHandler,
  1108.                  (long) gpSBP2DemoAppData,
  1109.                  false);
  1110.         if (err == noErr)
  1111.         {
  1112.             gpSBP2DemoAppData->openDocumentsEventHandlerInstalled =
  1113.                 true;
  1114.         }
  1115.     }
  1116.  
  1117.     // Create print documents event handler.
  1118.     if (err == noErr)
  1119.     {
  1120.         gpSBP2DemoAppData->printDocumentsEventHandler =
  1121.             NewAEEventHandlerProc (HandlePrintDocumentsEvent);
  1122.         if (gpSBP2DemoAppData->printDocumentsEventHandler == nil)
  1123.             err = memFullErr;
  1124.     }
  1125.  
  1126.     // Install handler.
  1127.     if (err == noErr)
  1128.     {
  1129.         err = AEInstallEventHandler
  1130.                 (kCoreEventClass,
  1131.                  kAEPrintDocuments,
  1132.                  gpSBP2DemoAppData->printDocumentsEventHandler,
  1133.                  (long) gpSBP2DemoAppData,
  1134.                  false);
  1135.         if (err == noErr)
  1136.         {
  1137.             gpSBP2DemoAppData->printDocumentsEventHandlerInstalled =
  1138.                 true;
  1139.         }
  1140.     }
  1141.  
  1142.     // Create quit application event handler.
  1143.     if (err == noErr)
  1144.     {
  1145.         gpSBP2DemoAppData->quitApplicationEventHandler =
  1146.             NewAEEventHandlerProc (HandleQuitApplicationEvent);
  1147.         if (gpSBP2DemoAppData->quitApplicationEventHandler == nil)
  1148.             err = memFullErr;
  1149.     }
  1150.  
  1151.     // Install handler.
  1152.     if (err == noErr)
  1153.     {
  1154.         err = AEInstallEventHandler
  1155.                 (kCoreEventClass,
  1156.                  kAEQuitApplication,
  1157.                  gpSBP2DemoAppData->quitApplicationEventHandler,
  1158.                  (long) gpSBP2DemoAppData,
  1159.                  false);
  1160.         if (err == noErr)
  1161.         {
  1162.             gpSBP2DemoAppData->quitApplicationEventHandlerInstalled =
  1163.                 true;
  1164.         }
  1165.     }
  1166.  
  1167.     // Create device added event handler.
  1168.     if (err == noErr)
  1169.     {
  1170.         gpSBP2DemoAppData->deviceAddedEventHandler =
  1171.             NewAEEventHandlerProc (HandleDeviceAddedEvent);
  1172.         if (gpSBP2DemoAppData->deviceAddedEventHandler == nil)
  1173.             err = memFullErr;
  1174.     }
  1175.  
  1176.     // Install handler.
  1177.     if (err == noErr)
  1178.     {
  1179.         err = AEInstallEventHandler
  1180.                 (kAESampleSBP2EventClass,
  1181.                  kAESampleSBP2DeviceAdded,
  1182.                  gpSBP2DemoAppData->deviceAddedEventHandler,
  1183.                  (long) gpSBP2DemoAppData,
  1184.                  false);
  1185.         if (err == noErr)
  1186.         {
  1187.             gpSBP2DemoAppData->deviceAddedEventHandlerInstalled =
  1188.                 true;
  1189.         }
  1190.     }
  1191.  
  1192.     // Create device removed event handler.
  1193.     if (err == noErr)
  1194.     {
  1195.         gpSBP2DemoAppData->deviceRemovedEventHandler =
  1196.             NewAEEventHandlerProc (HandleDeviceRemovedEvent);
  1197.         if (gpSBP2DemoAppData->deviceRemovedEventHandler == nil)
  1198.             err = memFullErr;
  1199.     }
  1200.  
  1201.     // Install handler.
  1202.     if (err == noErr)
  1203.     {
  1204.         err = AEInstallEventHandler
  1205.                 (kAESampleSBP2EventClass,
  1206.                  kAESampleSBP2DeviceRemoved,
  1207.                  gpSBP2DemoAppData->deviceRemovedEventHandler,
  1208.                  (long) gpSBP2DemoAppData,
  1209.                  false);
  1210.         if (err == noErr)
  1211.         {
  1212.             gpSBP2DemoAppData->deviceRemovedEventHandlerInstalled =
  1213.                 true;
  1214.         }
  1215.     }
  1216.  
  1217.     // Register with the family.
  1218.     if (err == noErr)
  1219.     {
  1220.         err = RegisterSBPClientApplication
  1221.                 (&(gpSBP2DemoAppData->sbpClientID),
  1222.                  (UInt32) gpSBP2DemoAppData);
  1223.     }
  1224.  
  1225.     // Get list of drivers.
  1226.     //zzz theoretically, new ones can be added while we're doing this.
  1227.     if (err == noErr)
  1228.         err = GetSBPDriverList (nil, 0, &numSBPDrivers);
  1229.  
  1230.     if ((err == noErr) && (numSBPDrivers > 0))
  1231.     {
  1232.         sbpDriverIDList = (SBPDriverID *)
  1233.             NewPtr (numSBPDrivers * sizeof (SBPDriverID));
  1234.         if (sbpDriverIDList != nil)
  1235.         {
  1236.             err = GetSBPDriverList
  1237.                     (sbpDriverIDList, numSBPDrivers, &numSBPDrivers);
  1238.         }
  1239.         else
  1240.         {
  1241.             err = memFullErr;
  1242.         }
  1243.     }
  1244.  
  1245.     // Create a device window for each driver.
  1246.     for (driverNum = 0;
  1247.          ((driverNum < numSBPDrivers) && (err == noErr));
  1248.          driverNum++)
  1249.     {
  1250.         err = CreateSBP2Demo (sbpDriverIDList[driverNum]);
  1251.     }
  1252.  
  1253.     // Clean up.
  1254.     if (sbpDriverIDList != nil)
  1255.         DisposePtr ((Ptr) sbpDriverIDList);
  1256.  
  1257.     return (err);
  1258. }
  1259.  
  1260.  
  1261. ////////////////////////////////////////////////////////////////////////////////
  1262. //
  1263. // QuitApplication
  1264. //
  1265. //   This routine quits the application.
  1266. //
  1267.  
  1268. static OSErr    QuitApplication(void)
  1269. {
  1270.     SBP2DemoDataPtr            pSBP2DemoData,
  1271.                                 pNextSBP2DemoData;
  1272.     SBPClientID                    sbpClientID;
  1273.     OSErr                        err = noErr;
  1274.  
  1275.     if (gpSBP2DemoAppData != nil)
  1276.     {
  1277.         // Remove all of the device windows.
  1278.         pSBP2DemoData = gpSBP2DemoAppData->sbp2DemoDataList;
  1279.         while (pSBP2DemoData != nil)
  1280.         {
  1281.             pNextSBP2DemoData = pSBP2DemoData->pNextSBP2DemoData;
  1282.     
  1283.             RemoveSBP2Demo (pSBP2DemoData->sbpDriverID);
  1284.     
  1285.             pSBP2DemoData = pNextSBP2DemoData;
  1286.         }
  1287.     
  1288.         // Unregister with the family.
  1289.         sbpClientID = gpSBP2DemoAppData->sbpClientID;
  1290.         if (sbpClientID != kInvalidSBPClientID)
  1291.             UnregisterSBPClientApplication (sbpClientID);
  1292.     
  1293.         // Uninstall Apple event handlers.
  1294.         // Uninstall device removed event handler.
  1295.         if (gpSBP2DemoAppData->deviceRemovedEventHandlerInstalled)
  1296.         {
  1297.             AERemoveEventHandler
  1298.                 (kAESampleSBP2EventClass,
  1299.                  kAESampleSBP2DeviceRemoved,
  1300.                  gpSBP2DemoAppData->deviceRemovedEventHandler,
  1301.                  false);
  1302.         }
  1303.  
  1304.         if (gpSBP2DemoAppData->deviceRemovedEventHandler != nil)
  1305.         {
  1306.             DisposeRoutineDescriptor
  1307.                 (gpSBP2DemoAppData->deviceRemovedEventHandler);
  1308.         }
  1309.  
  1310.         // Uninstall device added event handler.
  1311.         if (gpSBP2DemoAppData->deviceAddedEventHandlerInstalled)
  1312.         {
  1313.             AERemoveEventHandler
  1314.                 (kAESampleSBP2EventClass,
  1315.                  kAESampleSBP2DeviceAdded,
  1316.                  gpSBP2DemoAppData->deviceAddedEventHandler,
  1317.                  false);
  1318.         }
  1319.  
  1320.         if (gpSBP2DemoAppData->deviceAddedEventHandler != nil)
  1321.         {
  1322.             DisposeRoutineDescriptor
  1323.                 (gpSBP2DemoAppData->deviceAddedEventHandler);
  1324.         }
  1325.  
  1326.         // Uninstall quit application event handler.
  1327.         if (gpSBP2DemoAppData->quitApplicationEventHandlerInstalled)
  1328.         {
  1329.             AERemoveEventHandler
  1330.                 (kCoreEventClass,
  1331.                  kAEQuitApplication,
  1332.                  gpSBP2DemoAppData->quitApplicationEventHandler,
  1333.                  false);
  1334.         }
  1335.  
  1336.         if (gpSBP2DemoAppData->quitApplicationEventHandler != nil)
  1337.         {
  1338.             DisposeRoutineDescriptor
  1339.                 (gpSBP2DemoAppData->quitApplicationEventHandler);
  1340.         }
  1341.  
  1342.         // Uninstall print documents event handler.
  1343.         if (gpSBP2DemoAppData->printDocumentsEventHandlerInstalled)
  1344.         {
  1345.             AERemoveEventHandler
  1346.                 (kCoreEventClass,
  1347.                  kAEPrintDocuments,
  1348.                  gpSBP2DemoAppData->printDocumentsEventHandler,
  1349.                  false);
  1350.         }
  1351.  
  1352.         if (gpSBP2DemoAppData->printDocumentsEventHandler != nil)
  1353.         {
  1354.             DisposeRoutineDescriptor
  1355.                 (gpSBP2DemoAppData->printDocumentsEventHandler);
  1356.         }
  1357.  
  1358.         // Uninstall open documents event handler.
  1359.         if (gpSBP2DemoAppData->openDocumentsEventHandlerInstalled)
  1360.         {
  1361.             AERemoveEventHandler
  1362.                 (kCoreEventClass,
  1363.                  kAEOpenDocuments,
  1364.                  gpSBP2DemoAppData->openDocumentsEventHandler,
  1365.                  false);
  1366.         }
  1367.  
  1368.         if (gpSBP2DemoAppData->openDocumentsEventHandler != nil)
  1369.         {
  1370.             DisposeRoutineDescriptor
  1371.                 (gpSBP2DemoAppData->openDocumentsEventHandler);
  1372.         }
  1373.  
  1374.         // Uninstall open application event handler.
  1375.         if (gpSBP2DemoAppData->openApplicationEventHandlerInstalled)
  1376.         {
  1377.             AERemoveEventHandler
  1378.                 (kCoreEventClass,
  1379.                  kAEOpenApplication,
  1380.                  gpSBP2DemoAppData->openApplicationEventHandler,
  1381.                  false);
  1382.         }
  1383.  
  1384.         if (gpSBP2DemoAppData->openApplicationEventHandler != nil)
  1385.         {
  1386.             DisposeRoutineDescriptor
  1387.                 (gpSBP2DemoAppData->openApplicationEventHandler);
  1388.         }
  1389.  
  1390.         // Dispose of global data record.
  1391.         DisposePtr ((Ptr) gpSBP2DemoAppData);
  1392.         gpSBP2DemoAppData = nil;
  1393.  
  1394.         // Set event loop to quit.
  1395.         quitSBP2DemoApp = true;
  1396.     }
  1397.  
  1398.     return (err);
  1399. }
  1400.  
  1401.  
  1402. ////////////////////////////////////////////////////////////////////////////////
  1403. //
  1404. // CreateSBP2Demo
  1405. //
  1406. //   This routine creates a new device window for the given driver.
  1407. //
  1408.  
  1409. static OSErr    CreateSBP2Demo(
  1410.     SBPDriverID                    sbpDriverID)
  1411. {
  1412.     WindowRef                    sbp2DemoWindowRef = kInvalidWindowRef;
  1413.     SBP2DemoDataPtr                pSBP2DemoData = nil;
  1414.     OSErr                        err = noErr;
  1415.  
  1416.     // Get a new window.
  1417.     sbp2DemoWindowRef =
  1418.         GetNewWindow (kSBP2DemoWindowResourceID, nil, (WindowRef) -1);
  1419.     if (sbp2DemoWindowRef == kInvalidWindowRef)
  1420.         err = -1;//zzz not really correct.
  1421.  
  1422.     // Allocate device data record.
  1423.     if (err == noErr)
  1424.     {
  1425.         pSBP2DemoData =
  1426.             (SBP2DemoDataPtr) NewPtrClear (sizeof (SBP2DemoData));
  1427.         if (pSBP2DemoData != nil)
  1428.         {
  1429.             pSBP2DemoData->sbp2DemoWindowRef = sbp2DemoWindowRef;
  1430.             pSBP2DemoData->sbpDriverID = sbpDriverID;
  1431.             pSBP2DemoData->notificationCounter = 1129;  // force update first time
  1432.         }
  1433.         else
  1434.         {
  1435.             err = memFullErr;
  1436.         }
  1437.     }
  1438.  
  1439.     // Open connection to driver.
  1440.     if (err == noErr)
  1441.         err = OpenSBPDriver (sbpDriverID);
  1442.  
  1443.     // Store device data in window ref con.
  1444.     if (err == noErr)
  1445.         SetWRefCon (sbp2DemoWindowRef, (long) pSBP2DemoData);
  1446.  
  1447.     // Add device to list or clean up on error.
  1448.     if (err == noErr)
  1449.     {
  1450.         pSBP2DemoData->pNextSBP2DemoData =
  1451.             gpSBP2DemoAppData->sbp2DemoDataList;
  1452.         gpSBP2DemoAppData->sbp2DemoDataList =
  1453.             pSBP2DemoData;
  1454.     }
  1455.     else
  1456.     {
  1457.         if (pSBP2DemoData != nil)
  1458.             DisposePtr ((Ptr) pSBP2DemoData);
  1459.  
  1460.         if (sbp2DemoWindowRef != kInvalidWindowRef)
  1461.             CloseWindow (sbp2DemoWindowRef);
  1462.     }
  1463.  
  1464.     return (err);
  1465. }
  1466.  
  1467.  
  1468. ////////////////////////////////////////////////////////////////////////////////
  1469. //
  1470. // RemoveSBP2Demo
  1471. //
  1472. //   This routine removes the device window for the given driver.
  1473. //
  1474.  
  1475. static OSErr    RemoveSBP2Demo(
  1476.     SBPDriverID                    sbpDriverID)
  1477. {
  1478.     SBP2DemoDataPtr            pSBP2DemoData;
  1479.     Boolean                        found;
  1480.     OSErr                        err = noErr;
  1481.  
  1482.     // Find device for given driver.
  1483.     pSBP2DemoData = FindSBP2DemoFromSBPDriver (sbpDriverID);
  1484.     if (pSBP2DemoData != nil)
  1485.         found = true;
  1486.     else
  1487.         found = false;
  1488.  
  1489.     // Close connection to driver.
  1490.     if (found)
  1491.         CloseSBPDriver (sbpDriverID);
  1492.  
  1493.     // Close the device window.
  1494.     if (found)
  1495.         CloseWindow (pSBP2DemoData->sbp2DemoWindowRef);
  1496.  
  1497.     // Remove the device data from our list.
  1498.     if (found)
  1499.         RemoveSBP2DemoFromList (pSBP2DemoData);
  1500.  
  1501.     return (err);
  1502. }
  1503.  
  1504.  
  1505. ////////////////////////////////////////////////////////////////////////////////
  1506. //
  1507. // CloseSBP2DemoWindow
  1508. //
  1509. //   This routine closes the given device window.
  1510. //
  1511.  
  1512. static OSErr    CloseSBP2DemoWindow(
  1513.     WindowRef                    windowRef)
  1514. {
  1515.     SBP2DemoDataPtr            pSBP2DemoData;
  1516.     OSErr                        err = noErr;
  1517.  
  1518.     // Get device data.
  1519.     pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  1520.  
  1521.     // Remove the device.
  1522.     err = RemoveSBP2Demo (pSBP2DemoData->sbpDriverID);
  1523.     
  1524.     return (err);
  1525. }
  1526.  
  1527.  
  1528. ////////////////////////////////////////////////////////////////////////////////
  1529. //
  1530. // DrawSBP2Demo
  1531. //
  1532. //   This routine draws the given device window.
  1533. //
  1534.  
  1535. static OSErr    DrawSBP2Demo(
  1536.     WindowRef                    windowRef)
  1537. {
  1538.     SBP2DemoDataPtr                pSBP2DemoData;
  1539.     GrafPtr                        pWindowPort;
  1540.     Str255                        msg;
  1541.     int                            i;
  1542.     UInt8                        *p;
  1543.     OSErr                        err = noErr;
  1544.  
  1545.     // Get device data.
  1546.     pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  1547.  
  1548.     // Get window's graf port.
  1549.     pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  1550.  
  1551.     // Set port to the window's port.
  1552.     SetPortWindowPort (windowRef);
  1553.  
  1554.     // Clear window.
  1555.     EraseRect (&(pWindowPort->portRect));
  1556.     ForeColor (blackColor);
  1557.     TextSize (9);
  1558.     TextFont (4);        //monaco - why doesn't the enum work? 
  1559.  
  1560.     MoveTo (10, 20);
  1561.     DrawString ("\pLast Command: ");
  1562.     
  1563.     switch (pSBP2DemoData->lastCommand)
  1564.     {
  1565.         case kLoginMenuItem:            DrawString ("\pLogin     ");    break;
  1566.         case kLogoutMenuItem:            DrawString ("\pLogout    ");    break;
  1567.         case kQueryLoginsMenuItem:        DrawString ("\pQuery     ");    break;
  1568.         case kStatusInquiryMenuItem:    DrawString ("\pInquiry   ");    break;
  1569.         case kModeSenseMenuItem:        DrawString ("\pSense     ");    break;
  1570.         case kReadBlockMenuItem:        DrawString ("\pRead      ");    break;
  1571.         case kTargetResetMenuItem:        DrawString ("\pReset     ");    break;
  1572.         case kPowerTestMenuItem:        DrawString ("\pPowerTest ");    break;
  1573.         default:                        DrawString ("\p(none)    ");    break;
  1574.     }
  1575.  
  1576.     sprintf ((char *) msg, "x Status = %ld", pSBP2DemoData->lastError);
  1577.     msg [0] = strlen ((char *) msg) - 1;
  1578.     DrawString (msg);
  1579.     
  1580.     if (!pSBP2DemoData->lastError) DrawString ("\p (noErr)");
  1581.     
  1582.     MoveTo (10, 40);
  1583.  
  1584.     if (pSBP2DemoData->reconnecting)
  1585.     {
  1586.         DrawString ("\pRECONNECTING");
  1587.     }
  1588.     else
  1589.     if (pSBP2DemoData->reconnectFailed)
  1590.     {
  1591.         DrawString ("\pRECONNECT FAILED");
  1592.     }
  1593.     else
  1594.     {
  1595.         if (pSBP2DemoData->login)
  1596.             DrawString ("\pLogged in");
  1597.             else DrawString ("\pNot Logged in");
  1598.     }
  1599.  
  1600.     sprintf ((char *) msg, "x   (LUN = %ld)", pSBP2DemoData->lun);
  1601.     msg [0] = strlen ((char *) msg) - 1;
  1602.     DrawString (msg);
  1603.  
  1604.     MoveTo (10, 60);
  1605.     DrawString ("\pLast Normal Command ORB Status Notification: ");
  1606.  
  1607.     switch (pSBP2DemoData->notificationEvent)
  1608.     {
  1609.         case kSBP2NormalCommandStatus:    DrawString ("\pStatus      ");    break;
  1610.         case kSBP2NormalCommandTimeout:    DrawString ("\pTimeout     ");    break;
  1611.         case kSBP2UnsolicitedStatus:    DrawString ("\pUnsolicited ");    break;
  1612.         default:                        DrawString ("\p(none)      ");    break;
  1613.     }
  1614.  
  1615.     p = (UInt8 *) pSBP2DemoData->notificationMessage;
  1616.     if (pSBP2DemoData->notificationEvent && p)
  1617.     {
  1618.         MoveTo (20, 72);
  1619.         for (i = 0; i < pSBP2DemoData->notificationLength; i++)
  1620.         {
  1621.             sprintf ((char *) msg, "x%02lx ", (long) p[i]);
  1622.             msg [0] = strlen ((char *) msg) - 1;
  1623.             if (i < 16) DrawString (msg);
  1624.         }
  1625.         if (pSBP2DemoData->notificationLength > 16) DrawString ("\p...");
  1626.     }
  1627.  
  1628.     MoveTo (10, 92);
  1629.  
  1630.     if (pSBP2DemoData->lastCommand == kQueryLoginsMenuItem)
  1631.     {
  1632.         DrawString ("\pQuery Logins response:");
  1633.         MoveTo (20, 106);
  1634.         sprintf ((char *) msg, "xlength = %ld, max_logins = %ld",
  1635.                  pSBP2DemoData->sbpQueryLoginsParams.response[0] >> 16,
  1636.                  pSBP2DemoData->sbpQueryLoginsParams.response[0] & 0xffff);
  1637.         msg [0] = strlen ((char *) msg) - 1;
  1638.         DrawString (msg);
  1639.         
  1640.         for (i = 0; i < ((pSBP2DemoData->sbpQueryLoginsParams.response[0] >> 16) - 4) / 12; i++)
  1641.         {
  1642.             MoveTo (20, 120 + i*14);
  1643.             sprintf ((char *) msg, "xnode_ID = %04lx, login_ID = %04lx, EUI_64 = %08lx%08lx",
  1644.                      pSBP2DemoData->sbpQueryLoginsParams.response[1+i*3] >> 16,
  1645.                      pSBP2DemoData->sbpQueryLoginsParams.response[1+i*3] & 0xffff,
  1646.                      pSBP2DemoData->sbpQueryLoginsParams.response[2+i*3],
  1647.                      pSBP2DemoData->sbpQueryLoginsParams.response[3+i*3]);
  1648.             msg [0] = strlen ((char *) msg) - 1;
  1649.             DrawString (msg);
  1650.         }
  1651.         
  1652.         MoveTo (20, 120 + i*14);
  1653.         sprintf ((char *) msg, "xFWGetFWReferenceIDFromUniqueID status = %ld",
  1654.                  pSBP2DemoData->sbpQueryLoginsParams.uniqueIDStatus);
  1655.         msg [0] = strlen ((char *) msg) - 1;
  1656.         DrawString (msg);
  1657.  
  1658.         MoveTo (20, 134 + i*14);
  1659.         sprintf ((char *) msg, "xfwReferenceID = %08lx, unique ID = %08lx.%08lx",
  1660.                  pSBP2DemoData->sbpQueryLoginsParams.fwReferenceID,
  1661.                  pSBP2DemoData->sbpQueryLoginsParams.uniqueID.hi,
  1662.                  pSBP2DemoData->sbpQueryLoginsParams.uniqueID.lo);
  1663.         msg [0] = strlen ((char *) msg) - 1;
  1664.         DrawString (msg);
  1665.     }
  1666.     
  1667.     return (err);
  1668. }
  1669.  
  1670.  
  1671. ////////////////////////////////////////////////////////////////////////////////
  1672. //
  1673. // UpdateWindow
  1674. //
  1675. //   This routine updates the given window.
  1676. //
  1677.  
  1678. static OSErr    UpdateWindow(
  1679.     WindowRef                    windowRef)
  1680. {
  1681.     Boolean                        updateNeeded,
  1682.                                 preparedUpdate;
  1683.     OSErr                        err = noErr;
  1684.  
  1685.     // Prepare window for updating.
  1686.     err = PrepareWindowForUpdating (windowRef, &updateNeeded);
  1687.     if (err == noErr)
  1688.         preparedUpdate = true;
  1689.     else
  1690.         preparedUpdate = false;
  1691.  
  1692.     // Draw the window.
  1693.     if ((err == noErr) && (updateNeeded))
  1694.         err = DrawSBP2Demo (windowRef);
  1695.  
  1696.     // Release the window from updating.
  1697.     if (preparedUpdate)
  1698.         ReleaseWindowFromUpdating (windowRef);
  1699.  
  1700.     return (err);
  1701. }
  1702.  
  1703.  
  1704. ////////////////////////////////////////////////////////////////////////////////
  1705. //
  1706. // UpdateMenus
  1707. //
  1708. //   This routine sets up the menus for the front most window.
  1709. //
  1710.  
  1711. static OSErr    UpdateMenus(void)
  1712. {
  1713.     WindowRef                    windowRef;
  1714.     SBP2DemoDataPtr                pSBP2DemoData;
  1715.     MenuHandle                    hMenu;
  1716.     OSErr                        err = noErr;
  1717.  
  1718.     // Get reference to front most window.
  1719.     windowRef = FrontWindow ();
  1720.  
  1721.     // Get handle to file menu.
  1722.     hMenu = GetMenuHandle (kFileMenuID);
  1723.  
  1724.     // Close menu item should be enabled if there is a window open.
  1725.     if (windowRef != kInvalidWindowRef)
  1726.         EnableItem (hMenu, kCloseFileMenuItem);
  1727.     else
  1728.         DisableItem (hMenu, kCloseFileMenuItem);
  1729.  
  1730.     // Get handle to Command menu.
  1731.     hMenu = GetMenuHandle (kCommandMenuID);
  1732.  
  1733.     if (windowRef == kInvalidWindowRef)
  1734.     {
  1735.         DisableItem (hMenu, kLoginMenuItem);
  1736.         DisableItem (hMenu, kLogoutMenuItem);
  1737.         DisableItem (hMenu, kQueryLoginsMenuItem);
  1738.         DisableItem (hMenu, kStatusInquiryMenuItem);
  1739.         DisableItem (hMenu, kModeSenseMenuItem);
  1740.         DisableItem (hMenu, kReadBlockMenuItem);
  1741.         DisableItem (hMenu, kTargetResetMenuItem);
  1742.         DisableItem (hMenu, kPowerTestMenuItem);
  1743.     }
  1744.     else
  1745.     {
  1746.         pSBP2DemoData = (SBP2DemoDataPtr) GetWRefCon (windowRef);
  1747.         if (pSBP2DemoData->login)
  1748.         {
  1749.             DisableItem (hMenu, kLoginMenuItem);
  1750.             EnableItem (hMenu, kLogoutMenuItem);
  1751.             EnableItem (hMenu, kQueryLoginsMenuItem);
  1752.             EnableItem (hMenu, kStatusInquiryMenuItem);
  1753.             EnableItem (hMenu, kModeSenseMenuItem);
  1754.             EnableItem (hMenu, kReadBlockMenuItem);
  1755.             EnableItem (hMenu, kTargetResetMenuItem);
  1756.             EnableItem (hMenu, kPowerTestMenuItem);
  1757.         }
  1758.         else
  1759.         {
  1760.             EnableItem (hMenu, kLoginMenuItem);
  1761.             DisableItem (hMenu, kLogoutMenuItem);
  1762.             EnableItem (hMenu, kQueryLoginsMenuItem);
  1763.             DisableItem (hMenu, kStatusInquiryMenuItem);
  1764.             DisableItem (hMenu, kModeSenseMenuItem);
  1765.             DisableItem (hMenu, kReadBlockMenuItem);
  1766.             DisableItem (hMenu, kTargetResetMenuItem);
  1767.             DisableItem (hMenu, kPowerTestMenuItem);
  1768.         }
  1769. //        EnableItem (hMenu, kQueryLoginsMenuItem);
  1770.     }
  1771.     
  1772.     return (err);
  1773. }
  1774.  
  1775.  
  1776. ////////////////////////////////////////////////////////////////////////////////
  1777. //
  1778. // UpdateCursor
  1779. //
  1780. //   This routine sets the cursor to the correct image for the given mouse
  1781. // position.
  1782. //
  1783.  
  1784. static OSErr    UpdateCursor(
  1785.     Point                        mousePosition,
  1786.     RgnHandle                    hCursorRgn)
  1787. {
  1788.     WindowRef                    windowRef;
  1789.     GrafPtr                        pWindowPort;
  1790.     Rect                        windowPortRect;
  1791.     RgnHandle                    hArrowCursorRgn = nil,
  1792.                                 hPlusCursorRgn = nil;
  1793.     OSErr                        err = noErr;
  1794.  
  1795.     // Update if we're not in background.
  1796.     if (gpSBP2DemoAppData->inForeground)
  1797.     {
  1798.         // Get reference to front most window.
  1799.         windowRef = FrontWindow ();
  1800.  
  1801.         // Initialize cursor regions.
  1802.         hArrowCursorRgn = NewRgn ();
  1803.         hPlusCursorRgn = NewRgn ();
  1804.  
  1805.         // Set plus region to front most window if it's a device.
  1806.         if (IsSBP2DemoWindow (windowRef))
  1807.         {
  1808.             // Get window's port and port rect.
  1809.             pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  1810.  
  1811.             // Set plus region to visible portion of device window.
  1812.             SetPortWindowPort (windowRef);
  1813.             SetOrigin (-(pWindowPort->portBits.bounds.left),
  1814.                        -(pWindowPort->portBits.bounds.top));
  1815.             windowPortRect = pWindowPort->portRect;
  1816.             RectRgn (hPlusCursorRgn, &windowPortRect);
  1817.             SectRgn (hPlusCursorRgn, pWindowPort->visRgn, hPlusCursorRgn);
  1818.             SetOrigin (0, 0);
  1819.         }
  1820.  
  1821.         // Set arrow region to everywhere except the other cursor regions.
  1822.         SetRectRgn (hArrowCursorRgn,
  1823.                     kExtremeNeg,
  1824.                     kExtremeNeg,
  1825.                     kExtremePos,
  1826.                     kExtremePos);
  1827.         DiffRgn (hArrowCursorRgn, hPlusCursorRgn, hArrowCursorRgn);
  1828.  
  1829.         // Change the cursor image and region.
  1830.         if (PtInRgn (mousePosition, hPlusCursorRgn))
  1831.         {
  1832.             SetCursor (*GetCursor (plusCursor));
  1833.             CopyRgn (hPlusCursorRgn, hCursorRgn);
  1834.         }
  1835.         else
  1836.         {
  1837.             SetCursor (&(gpSBP2DemoAppData->qdGlobals.arrow));
  1838.             CopyRgn (hArrowCursorRgn, hCursorRgn);
  1839.         }
  1840.     }
  1841.  
  1842.     // Clean up.
  1843.     if (hArrowCursorRgn != nil)
  1844.         DisposeRgn (hArrowCursorRgn);
  1845.     if (hPlusCursorRgn != nil)
  1846.         DisposeRgn (hPlusCursorRgn);
  1847.  
  1848.     return (err);
  1849. }
  1850.  
  1851.  
  1852. ////////////////////////////////////////////////////////////////////////////////
  1853. //
  1854. // IsSBP2DemoWindow
  1855. //
  1856. //   This routine returns true if the given window is a SBP2 device.
  1857. //
  1858.  
  1859. static Boolean    IsSBP2DemoWindow(
  1860.     WindowRef                    windowRef)
  1861. {
  1862.     // If the window is a user window, it's a device.
  1863.     if (GetWindowKind (windowRef) == kApplicationWindowKind)
  1864.         return (true);
  1865.     else
  1866.         return (false);
  1867. }
  1868.  
  1869.  
  1870. ////////////////////////////////////////////////////////////////////////////////
  1871. //
  1872. // GetMousePosition
  1873. //
  1874. //   This routine returns the current position of the mouse.
  1875. //
  1876.  
  1877. static void    GetMousePosition(
  1878.     Point                        *pMousePosition)
  1879. {
  1880.     EventRecord                    eventRecord;
  1881.  
  1882.     // Get mouse position from event manager.
  1883.     OSEventAvail (0, &eventRecord);
  1884.     *pMousePosition = eventRecord.where;
  1885. }
  1886.  
  1887.  
  1888. ////////////////////////////////////////////////////////////////////////////////
  1889. //
  1890. // HandleOpenApplicationEvent
  1891. //
  1892. //   This routine handles open application events.
  1893. //
  1894.  
  1895. static pascal OSErr    HandleOpenApplicationEvent(
  1896.     AppleEvent                    *theAppleEvent,
  1897.     AppleEvent                    *reply,
  1898.     long                        handlerRefcon)
  1899. {
  1900.     OSErr                        err = noErr;
  1901.  
  1902.     return (err);
  1903. }
  1904.  
  1905.  
  1906. ////////////////////////////////////////////////////////////////////////////////
  1907. //
  1908. // HandleOpenDocumentsEvent
  1909. //
  1910. //   This routine handles open documents events.
  1911. //zzz should we return an error?
  1912. //
  1913.  
  1914. static pascal OSErr    HandleOpenDocumentsEvent(
  1915.     AppleEvent                    *theAppleEvent,
  1916.     AppleEvent                    *reply,
  1917.     long                        handlerRefcon)
  1918. {
  1919.     OSErr                        err = noErr;
  1920.  
  1921.     return (err);
  1922. }
  1923.  
  1924.  
  1925. ////////////////////////////////////////////////////////////////////////////////
  1926. //
  1927. // HandlePrintDocumentsEvent
  1928. //
  1929. //   This routine handles print documents events.
  1930. //zzz should we return an error?
  1931. //
  1932.  
  1933. static pascal OSErr    HandlePrintDocumentsEvent(
  1934.     AppleEvent                    *theAppleEvent,
  1935.     AppleEvent                    *reply,
  1936.     long                        handlerRefcon)
  1937. {
  1938.     OSErr                        err = noErr;
  1939.  
  1940.     return (err);
  1941. }
  1942.  
  1943.  
  1944. ////////////////////////////////////////////////////////////////////////////////
  1945. //
  1946. // HandleQuitApplicationEvent
  1947. //
  1948. //   This routine handles quit application events.
  1949. //
  1950.  
  1951. static pascal OSErr    HandleQuitApplicationEvent(
  1952.     AppleEvent                    *theAppleEvent,
  1953.     AppleEvent                    *reply,
  1954.     long                        handlerRefcon)
  1955. {
  1956.     OSErr                        err = noErr;
  1957.  
  1958.     // Quit the application.
  1959.     QuitApplication ();
  1960.  
  1961.     return (err);
  1962. }
  1963.  
  1964.  
  1965. ////////////////////////////////////////////////////////////////////////////////
  1966. //
  1967. // HandleDeviceAddedEvent
  1968. //
  1969. //   This routine handles device added events.
  1970. //
  1971.  
  1972. static pascal OSErr    HandleDeviceAddedEvent(
  1973.     AppleEvent                    *theAppleEvent,
  1974.     AppleEvent                    *reply,
  1975.     long                        handlerRefcon)
  1976. {
  1977.     SBPDriverID                    sbpDriverID;
  1978.     DescType                    returnedType;
  1979.     Size                        returnedSize;
  1980.     Boolean                        inList;
  1981.     OSErr                        err = noErr;
  1982.  
  1983.     // Get driver ID from apple event.
  1984.     err = AEGetParamPtr (theAppleEvent,
  1985.                          kAESBPDriverIDKey,
  1986.                          kAESBPDriverIDType,
  1987.                          &returnedType,
  1988.                          &sbpDriverID,
  1989.                          sizeof (SBPDriverID),
  1990.                          &returnedSize);
  1991.  
  1992.     // Check if sbp driver is in our list.  This will happen if device was
  1993.     // connected, opened, disconnected, and reconnected without being closed.
  1994.     // If it's in our list, do not create a new device.
  1995.     if (err == noErr)
  1996.     {
  1997.         if (FindSBP2DemoFromSBPDriver (sbpDriverID))
  1998.             inList = true;
  1999.         else
  2000.             inList = false;
  2001.     }
  2002.  
  2003.     // Create device for added device.
  2004.     if ((err == noErr) && (!inList))
  2005.         err = CreateSBP2Demo (sbpDriverID);
  2006.  
  2007.     return (err);
  2008. }
  2009.  
  2010.  
  2011. ////////////////////////////////////////////////////////////////////////////////
  2012. //
  2013. // HandleDeviceRemovedEvent
  2014. //
  2015. //   This routine handles device removed events.
  2016. //
  2017.  
  2018. static pascal OSErr    HandleDeviceRemovedEvent(
  2019.     AppleEvent                    *theAppleEvent,
  2020.     AppleEvent                    *reply,
  2021.     long                        handlerRefcon)
  2022. {
  2023.     SBPDriverID                    sbpDriverID;
  2024.     DescType                    returnedType;
  2025.     Size                        returnedSize;
  2026.     OSErr                        err = noErr;
  2027.  
  2028.     // Get driver ID from apple event.
  2029.     err = AEGetParamPtr (theAppleEvent,
  2030.                          kAESBPDriverIDKey,
  2031.                          kAESBPDriverIDType,
  2032.                          &returnedType,
  2033.                          &sbpDriverID,
  2034.                          sizeof (SBPDriverID),
  2035.                          &returnedSize);
  2036.  
  2037.     // Create device for removed device.
  2038.     if (err == noErr)
  2039.         err = RemoveSBP2Demo (sbpDriverID);
  2040.  
  2041.     return (err);
  2042. }
  2043.  
  2044.  
  2045. ////////////////////////////////////////////////////////////////////////////////
  2046. //
  2047. // FindSBP2DemoFromSBPDriver
  2048. //
  2049. //   This routine searches for the device corresponding to the given
  2050. // SBP driver ID.
  2051. //
  2052.  
  2053. static SBP2DemoDataPtr    FindSBP2DemoFromSBPDriver(
  2054.     SBPDriverID                    sbpDriverID)
  2055. {
  2056.     SBP2DemoDataPtr            pSBP2DemoData,
  2057.                                 pPrevSBP2DemoData;
  2058.     Boolean                        found;
  2059.     OSErr                        err = noErr;
  2060.  
  2061.     // Find device for given driver.
  2062.     pSBP2DemoData = gpSBP2DemoAppData->sbp2DemoDataList;
  2063.     pPrevSBP2DemoData = nil;
  2064.     found = false;
  2065.     while ((pSBP2DemoData != nil) && (!found))
  2066.     {
  2067.         if (pSBP2DemoData->sbpDriverID == sbpDriverID)
  2068.         {
  2069.             found = true;
  2070.         }
  2071.         else
  2072.         {
  2073.             pPrevSBP2DemoData = pSBP2DemoData;
  2074.             pSBP2DemoData = pSBP2DemoData->pNextSBP2DemoData;
  2075.         }
  2076.     }
  2077.  
  2078.     return (pSBP2DemoData);
  2079. }
  2080.  
  2081.  
  2082. ////////////////////////////////////////////////////////////////////////////////
  2083. //
  2084. // RemoveSBP2DemoFromList
  2085. //
  2086. //   This routine removes the given device data record from our list.
  2087. //
  2088.  
  2089. static OSErr    RemoveSBP2DemoFromList(
  2090.     SBP2DemoDataPtr            pSBP2DemoData)
  2091. {
  2092.     SBP2DemoDataPtr            pSearchSBP2DemoData,
  2093.                                 pPrevSBP2DemoData;
  2094.     Boolean                        found;
  2095.     OSErr                        err = noErr;
  2096.  
  2097.     // Find previous device data record.
  2098.     pSearchSBP2DemoData = gpSBP2DemoAppData->sbp2DemoDataList;
  2099.     pPrevSBP2DemoData = nil;
  2100.     found = false;
  2101.     while ((pSearchSBP2DemoData != nil) && (!found))
  2102.     {
  2103.         if (pSearchSBP2DemoData == pSBP2DemoData)
  2104.         {
  2105.             found = true;
  2106.         }
  2107.         else
  2108.         {
  2109.             pPrevSBP2DemoData = pSearchSBP2DemoData;
  2110.             pSearchSBP2DemoData = pSearchSBP2DemoData->pNextSBP2DemoData;
  2111.         }
  2112.     }
  2113.  
  2114.     // Remove the device data from our list.
  2115.     if (found)
  2116.     {
  2117.         if (pPrevSBP2DemoData != nil)
  2118.         {
  2119.             pPrevSBP2DemoData->pNextSBP2DemoData =
  2120.                 pSBP2DemoData->pNextSBP2DemoData;
  2121.         }
  2122.         else
  2123.         {
  2124.             gpSBP2DemoAppData->sbp2DemoDataList =
  2125.                 pSBP2DemoData->pNextSBP2DemoData;
  2126.         }
  2127.  
  2128.         DisposePtr ((Ptr) pSBP2DemoData);
  2129.     }
  2130.  
  2131.     return (err);
  2132. }
  2133.  
  2134.  
  2135. ////////////////////////////////////////////////////////////////////////////////
  2136. ////////////////////////////////////////////////////////////////////////////////
  2137. //
  2138. // Window managing routines.
  2139. //
  2140. //   Code that needs to use window records goes here.
  2141. //
  2142. ////////////////////////////////////////////////////////////////////////////////
  2143. ////////////////////////////////////////////////////////////////////////////////
  2144.  
  2145. ////////////////////////////////////////////////////////////////////////////////
  2146. //
  2147. // PrepareWindowForUpdating
  2148. //
  2149. //   This routine prepares the given window for updating.
  2150. //
  2151.  
  2152. static OSErr    PrepareWindowForUpdating(
  2153.     WindowRef                    windowRef,
  2154.     Boolean                        *pUpdateNeeded)
  2155. {
  2156.     GrafPtr                        pWindowPort;
  2157.     OSErr                        err = noErr;
  2158.  
  2159.     // Get window's port.
  2160.     pWindowPort = (GrafPtr) GetWindowPort (windowRef);
  2161.  
  2162.     // Call window manager to begin update.
  2163.     BeginUpdate (windowRef);
  2164.  
  2165.     // Check if window needs updating.
  2166.     if (pUpdateNeeded != nil)
  2167.     {
  2168.         if (EmptyRgn (pWindowPort->visRgn))
  2169.             *pUpdateNeeded = false;
  2170.         else
  2171.             *pUpdateNeeded = true;
  2172.     }
  2173.  
  2174.     return (err);
  2175. }
  2176.  
  2177.  
  2178. ////////////////////////////////////////////////////////////////////////////////
  2179. //
  2180. // ReleaseWindowFromUpdating
  2181. //
  2182. //   This routine releases the given window from updating.
  2183. //
  2184.  
  2185. static OSErr    ReleaseWindowFromUpdating(
  2186.     WindowRef                    windowRef)
  2187. {
  2188.     OSErr                        err = noErr;
  2189.  
  2190.     // Call window manager to end update.
  2191.     EndUpdate (windowRef);
  2192.  
  2193.     return (err);
  2194. }
  2195.  
  2196.  
  2197. ////////////////////////////////////////////////////////////////////////////////
  2198. //
  2199. // CheckAllDevices
  2200. //
  2201. //   This routine checks all devices to see if their status has changed.
  2202. //
  2203.  
  2204. static void    CheckAllDevices (void)
  2205. {
  2206.     SBP2DemoDataPtr                pSBP2DemoData;
  2207.     SBPStatusParams                sbpStatusParams;
  2208.     Boolean                        oldReconnecting, oldReconnectFailed, oldLogin;
  2209.     UInt32                        oldNotificationCounter;
  2210.     GrafPtr                        pWindowPort;
  2211.     OSErr                        err = noErr;
  2212.  
  2213.     pSBP2DemoData = gpSBP2DemoAppData->sbp2DemoDataList;
  2214.     while (pSBP2DemoData)
  2215.     {
  2216.         oldLogin = pSBP2DemoData->login;
  2217.         oldReconnecting = pSBP2DemoData->reconnecting;
  2218.         oldReconnectFailed = pSBP2DemoData->reconnectFailed;
  2219.         oldNotificationCounter = pSBP2DemoData->notificationCounter;
  2220.         
  2221.         sbpStatusParams.sbpInterfaceParams.interfaceSelector = kSampleSBPStatus;
  2222.  
  2223.         err = CallSBPDriver (pSBP2DemoData->sbpDriverID,
  2224.                              (SBPInterfaceParamsPtr) &sbpStatusParams);
  2225.         
  2226.         if (err == noErr)
  2227.         {
  2228.             if ((sbpStatusParams.login != oldLogin) ||
  2229.                 (sbpStatusParams.reconnecting != oldReconnecting) ||
  2230.                 (sbpStatusParams.reconnectFailed != oldReconnectFailed) ||
  2231.                 (sbpStatusParams.notificationCounter != oldNotificationCounter))
  2232.             {
  2233.                 pSBP2DemoData->login = sbpStatusParams.login;
  2234.                 pSBP2DemoData->reconnecting = sbpStatusParams.reconnecting;
  2235.                 pSBP2DemoData->reconnectFailed = sbpStatusParams.reconnectFailed;
  2236.                 pSBP2DemoData->notificationCounter = sbpStatusParams.notificationCounter;
  2237.                 pSBP2DemoData->notificationEvent = sbpStatusParams.notificationEvent;
  2238.                 pSBP2DemoData->notificationLength = sbpStatusParams.notificationLength;
  2239.                 pSBP2DemoData->notificationMessage = sbpStatusParams.notificationMessage;
  2240.                 pSBP2DemoData->lun = sbpStatusParams.lun;
  2241.                 
  2242.                 // Force an update on the window
  2243.                 pWindowPort = (GrafPtr) GetWindowPort (pSBP2DemoData->sbp2DemoWindowRef);
  2244.                 SetPortWindowPort (pSBP2DemoData->sbp2DemoWindowRef);
  2245.                 InvalRect (&(pWindowPort->portRect));
  2246.             }
  2247.         }
  2248.         
  2249.         pSBP2DemoData = pSBP2DemoData->pNextSBP2DemoData;
  2250.     }
  2251. }
  2252.  
  2253.  
  2254.